(function() {
	
	var defaultUserPic = "../img/defaultUserPic.png";
	$("#UserPic").error(function() {
		$(this).attr("src", defaultUserPic);
		$(this).error = null;
	});
	
	$(document).ready(function() {
		getContent();
	});

	function getContent(){
		var hierarchy = jQuery('#hierarchy').val();
		var url = window.rootPath + '/ui/taskSupervision/findAllTaskSupervision';
		jQuery.ajax({
			url : encodeURI(url),
			cache : false,
			type : "get",
			data : "",
			dataType : "json",
			contentType : 'application/json;charset=utf-8',// 解决返回数据中的中文乱码问题
			success : function(data) {
				var receives = data.receives;
				var deals = data.deals;
				setReceiveListView(receives);
				setDealListView(deals);
			}
		})
	}
	
	function setReceiveListView(pageInfo){
		$("#receiveList").empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无接收超时任务信息</h2></li>';
			$("#receiveList").append(contenthtml);
			$("#receiveList").listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			
			var purl = pageInfo[i].registerEventImgAdress;
			if (purl == undefined || purl == "") {
				purl = "img/nopicture.png";
			}
			purl = window.rootPath + "/" + purl;
			var eventTaskMemo=pageInfo[i].registerEventMemo;
			if(eventTaskMemo==''||undefined==eventTaskMemo){
				eventTaskMemo="该事件无备注";
			}
			var s='<img width="4%" src="/wcsi/img/yourlocation.png"/>'+pageInfo[i].registerEventAdress;
//			var lefthtml='<div class="leftImg"><img src="'+purl+'" style="margin-top:0;margin-left:0;width:60px;height:60px"/></div>'
//			+ '<div class="leftContent"><div class="title"><font color="red">'+moment(pageInfo[i].eventTaskTime).format('YYYY-MM-DD HH:mm')+'</font>&nbsp;&nbsp'+ pageInfo[i].registerEventName+'</div>'
//			+ '<div class="content"><p style="color:#999999;">&nbsp;'+pageInfo[i].registerEventName+'&nbsp;&nbsp;&nbsp;&nbsp;'+pageInfo[i].registerEventSeriousLevel.title+'</p>'
//			+ '<p style="color:#999999;">&nbsp;'+eventTaskMemo+'<br/>'+s+'</p></div></div>';
			var lefthtml = ''
				+ '<div class="listViewLocalImg">'
				+ '<img src="'+purl+'"/>'
				+ '</div>'
				+ '<div class="listViewContent">'
				+ '<p class="listViewTitle margin-Title"><font color="red">'+moment(pageInfo[i].eventTaskTime).format('YYYY-MM-DD HH:mm')+'</font>&nbsp;&nbsp'+ pageInfo[i].registerEventName+'</p>'
				+ '<p class="listViewMemo margin-Title">&nbsp;'+pageInfo[i].registerEventName+'&nbsp;&nbsp;&nbsp;&nbsp;'+pageInfo[i].registerEventSeriousLevel.title
				+ '<br/>&nbsp;'+eventTaskMemo+'</p>'
				+ '</div>'
				+ '<div class="listViewLocal">'+s+'</div>';
			var contenthtml='<li class="touch"><div class="item" id="receive'+i+'">'
			+ '<div class="left-view" dataId="'+pageInfo[i].id+'">'
			+ lefthtml
			+ '</div>'
			+ '<div class="right-view">';
			contenthtml += '<span class="info"><font>督办</font></span>';
			contenthtml += '<span class="confirm" item="receive'+i+'" dataId="'+pageInfo[i].id+'"><font>确认督办</font></span>';
			contenthtml += '</div>'
			+ '</div></li>';
			
			$("#receiveList").append(contenthtml);
		}
		$("#receiveList").listview('refresh');
		for(var i=0;i<pageInfo.length;i++){
			$("#"+pageInfo[i].id).removeClass("ui-btn-icon-right");
		}
		
	}

	$("#receiveList").on("swipeleft", ".item", function (){
		 $("#receiveList").each(function(){
			 $(this).find('.item').removeClass('selected');
		 });
		$(this).addClass('selected');
	});
	
	$("#receiveList").on("swiperight", ".item", function (){
		$(this).find('.confirmselect').removeClass("confirmselect").addClass("confirmremove").removeClass("confirmremove");
		$(this).removeClass('selected');
	});

	$("#receiveList").on("click", ".info", function (){
		$(this).next().removeClass("confirmremove").addClass('confirmselect');
	});

	$("#receiveList").on("click", ".confirm", function (){
		var dataId = $(this).attr('dataId');
		var item = $(this).attr('item');
		$(this).removeClass("confirmselect").addClass("confirmremove").removeClass("confirmremove");
		$('#'+item).removeClass('selected');
		supervision(dataId,'receive');
	});
	
	$("#receiveList").on("click", ".left-view", function (){
		$("#receiveList").each(function(){
			$(this).find('.item').removeClass('selected');
			$(this).find('.confirmselect').removeClass("confirmselect").addClass("confirmremove").removeClass("confirmremove");
		});
		var id = $(this).attr("dataId");
		var url = window.rootPath + '/ui/taskSupervision/getTaskById/'+id;
		jQuery.ajax({
			url : encodeURI(url),
			type : "post",
			dataType : "json",
			data : '',
			contentType : "application/json;charset=UTF-8",
			success : function(data) {
				cleanRegisterPage();
				var register = data.eventRegister;
				$("#registerId").val(register.id);
				$("#type").val("receive");
				$("#registerEventName").html(register.registerEventName);
				$("#registerEventContent").html(register.registerEventContent);
				$("#registerEventMemo").html(register.registerEventMemo);
				$("#registerEventIdentify").html(register.registerEventIdentify);
				$("#registerEventType").html(register.registerEventType.eventName);
				$("#registerEventEvaluateLevel").html(register.registerEventEvaluateLevel.gradingName);
				$("#registerEventAccTime").html(moment(register.registerEventAccTime).format("YYYY-MM-DD HH:mm"));
				var where=register.registerEventAdress;
				if(where==null||''==where||undefined==where){
					where="上报时未获取到上报地点";
				}
				$("#where").html(where);
				// 地图
				var lon = data.longitude;
				var lat = data.latitude

				createFlickerPoint(register, "#waitRegister");
				setTimeout(function() {
					setCenter("#waitRegister");
				}, 500);
				
				$.mobile.changePage('#RegisterPage');
			}
		});
		
	});
	
	function setDealListView(pageInfo){
		$("#dealList").empty();
		if (pageInfo.length == 0) {
			var contenthtml = '<li><h2 style="color:blue">暂无处理超时任务信息</h2></li>';
			$("#dealList").append(contenthtml);
			$("#dealList").listview('refresh');
		}
		for (var i = 0; i < pageInfo.length; i++) {
			
			var purl = pageInfo[i].register.registerEventImgAdress;
			if (purl == undefined || purl == "") {
				purl = "img/nopicture.png";
			}
			purl = window.rootPath + "/" + purl;
			var eventTaskMemo=pageInfo[i].register.registerEventMemo;
			if(eventTaskMemo==''||undefined==eventTaskMemo){
				eventTaskMemo="该事件无备注";
			}
			var s='<img width="4%" src="/wcsi/img/yourlocation.png"/>'+pageInfo[i].register.registerEventAdress;
//			var lefthtml='<div class="leftImg"><img src="'+purl+'" style="margin-top:0;margin-left:0;width:60px;height:60px"/></div>'
//			+ '<div class="leftContent"><div class="title"><font color="red">'+moment(pageInfo[i].register.eventTaskTime).format('YYYY-MM-DD HH:mm')+'</font>&nbsp;&nbsp'+ pageInfo[i].register.registerEventName+'</div>'
//			+ '<div class="content"><p style="color:#999999;">&nbsp;'+pageInfo[i].register.registerEventName+'&nbsp;&nbsp;&nbsp;&nbsp;'+pageInfo[i].register.registerEventSeriousLevel.title+'</p>'
//			+ '<p style="color:#999999;">&nbsp;'+eventTaskMemo+'<br/>'+s+'</p></div></div>';

			var lefthtml = ''
			+ '<div class="listViewLocalImg">'
			+ '<img src="'+purl+'"/>'
			+ '</div>'
			+ '<div class="listViewContent">'
			+ '<p class="listViewTitle margin-Title"><font color="red">'+moment(pageInfo[i].register.eventTaskTime).format('YYYY-MM-DD HH:mm')+'</font>&nbsp;&nbsp'+ pageInfo[i].register.registerEventName+'</p>'
			+ '<p class="listViewMemo margin-Title">&nbsp;'+pageInfo[i].register.registerEventName+'&nbsp;&nbsp;&nbsp;&nbsp;'+pageInfo[i].register.registerEventSeriousLevel.title
			+ '<br/>&nbsp;'+eventTaskMemo+'</p>'
			+ '</div>'
			+ '<div class="listViewLocal">'+s+'</div>';
			var contenthtml='<li class="touch"><div class="item" id="deal'+i+'">'
			+ '<div class="left-view" dataId="'+pageInfo[i].register.id+'">'
			+ lefthtml
			+ '</div>'
			+ '<div class="right-view">';
			contenthtml += '<span class="info"><font>督办</font></span>';
			contenthtml += '<span class="confirm" item="deal'+i+'" dataId="'+pageInfo[i].register.id+'"><font>确认督办</font></span>';
			contenthtml += '</div>'
				+ '</div></li>';
			
			$("#dealList").append(contenthtml);
		}
		$("#dealList").listview('refresh');
		for(var i=0;i<pageInfo.length;i++){
			$("#"+pageInfo[i].id).removeClass("ui-btn-icon-right");
		}
		
	}
	
	$("#dealList").on("swipeleft", ".item", function (){
		$("#dealList").each(function(){
			$(this).find('.item').removeClass('selected');
		});
		$(this).addClass('selected');
	});
	
	$("#dealList").on("swiperight", ".item", function (){
		$(this).find('.confirmselect').removeClass("confirmselect").addClass("confirmremove").removeClass("confirmremove");
		$(this).removeClass('selected');
	});
	
	$("#dealList").on("click", ".info", function (){
		$(this).next().removeClass("confirmremove").addClass('confirmselect');
	});
	
	$("#dealList").on("click", ".confirm", function (){
		var dataId = $(this).attr('dataId');
		var item = $(this).attr('item');
		$(this).removeClass("confirmselect").addClass("confirmremove").removeClass("confirmremove");
		$('#'+item).removeClass('selected');
		supervision(dataId,'deal');
	});
	
	$("#dealList").on("click", ".left-view", function (){
		$("#dealList").each(function(){
			$(this).find('.item').removeClass('selected');
			$(this).find('.confirmselect').removeClass("confirmselect").addClass("confirmremove").removeClass("confirmremove");
		});
		var id = $(this).attr("dataId");
		var url = window.rootPath + '/ui/taskSupervision/getTaskById/'+id;
		jQuery.ajax({
			url : encodeURI(url),
			type : "post",
			dataType : "json",
			data : '',
			contentType : "application/json;charset=UTF-8",
			success : function(data) {
				cleanRegisterPage();
				var register = data.eventRegister;
				$("#registerId").val(register.id);
				$("#type").val("deal");
				$("#registerEventName").html(register.registerEventName);
				$("#registerEventContent").html(register.registerEventContent);
				$("#registerEventMemo").html(register.registerEventMemo);
				$("#registerEventIdentify").html(register.registerEventIdentify);
				$("#registerEventType").html(register.registerEventType.eventName);
				$("#registerEventEvaluateLevel").html(register.registerEventEvaluateLevel.gradingName);
				$("#registerEventAccTime").html(moment(register.registerEventAccTime).format("YYYY-MM-DD HH:mm"));
				var where=register.registerEventAdress;
				if(where==null||''==where||undefined==where){
					where="上报时未获取到上报地点";
				}
				$("#where").html(where);
				// 地图
				var lon = data.longitude;
				var lat = data.latitude
				
				createFlickerPoint(register, "#waitRegister");
				setTimeout(function() {
					setCenter("#waitRegister");
				}, 500);
				
				$.mobile.changePage('#RegisterPage');
			}
		});
		
	});
	
	function cleanRegisterPage(){
		$("#registerId").val('');
		$("#type").val('');
		$("#registerEventName").html('');
		$("#registerEventContent").html('');
		$("#registerEventMemo").html('');
		$("#registerEventIdentify").html('');
		$("#registerEventType").html('');
		$("#registerEventEvaluateLevel").html('');
		$("#registerEventAccTime").html('');
		$("#where").html('');
	}
	
	function supervision(id,type){
		var url = window.rootPath + '/ui/taskSupervision/supervision/'+type+'/'+id;
		jQuery.ajax({
			url : encodeURI(url),
			type : "post",
			dataType : "json",
			data : '',
			contentType : "application/json;charset=UTF-8",
			success : function(data) {
				alert(data.msg);
				$.mobile.changePage('#pageone');
			}
		});
	}
	
	$("#dbBtn").on("click", function (){
		var id = $("#registerId").val();
		var type = $("#type").val();
		supervision(id, type);
	});
	
})();